package indi.kider.server.woniubus.service;

import com.alibaba.fastjson.JSON;
import indi.kider.common.constant.LeaseBusStateCons;
import indi.kider.common.model.apabus.LeaseBus;
import indi.kider.common.model.apabus.Rent;
import indi.kider.common.model.employee.Tenant;
import indi.kider.common.model.woniubus.LeaseBusAndRent;
import indi.kider.common.model.woniubus.LeaseBusQuery;
import indi.kider.common.model.woniubus.TenantVo;
import indi.kider.server.apabus.mapper.LeaseBusMapper;
import indi.kider.server.employee.mapper.TenantMapper;
import indi.kider.server.woniubus.service.related.BillService;
import indi.kider.server.woniubus.service.related.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.model.base.CoreException;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageUtil;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
@Service
public class LeaseBusServiceImpl implements LeaseBusService {


    @Autowired
    private LeaseBusMapper leaseBusMapper;
    @Autowired
    private WoniuBusService woniuBusService;

    @Autowired
    private SmsService smsService;
    @Autowired
    private BillService billService;
    @Autowired
    private TenantMapper tenantMapper;

    @Override
    public List<Map> queryLeaseBusListForSh(PageRequest<LeaseBusQuery> request) {
        PageUtil.start(request);
        return leaseBusMapper.queryLeaseBusListForSh(request.getQuery());
    }

    @Override
    public CommonResponse sh(LeaseBusQuery leaseBus) throws Exception {
        leaseBus.setBusState(LeaseBusStateCons.REVIEW);
        leaseBusMapper.updateByPrimaryKey(leaseBus);
        try{
            //发送短信
            smsService.shSuccessSms(leaseBus);
        }catch (Exception e){
        }
        return CommonResponse.buildUpdateSuccessResponse("审核成功,提醒用户费用结算!",null);
    }
    @Override
    public CommonResponse shErr(LeaseBusQuery leaseBus) throws Exception {
        leaseBus.setBusState(LeaseBusStateCons.END);
        leaseBusMapper.updateByPrimaryKey(leaseBus);
        return CommonResponse.buildUpdateSuccessResponse("审核驳回,提醒用户重新申请!",null);
    }

    @Override
    public CommonResponse gjshErr(LeaseBusQuery leaseBus) throws Exception {
        leaseBus.setBusState(LeaseBusStateCons.GJERR);
        leaseBusMapper.updateByPrimaryKey(leaseBus);
        return CommonResponse.buildUpdateSuccessResponse("管家审核驳回,提醒用户重新申请!",null);
    }

    @Override
    public List<Map> queryLeaseBusListForGjSh(PageRequest<LeaseBusQuery> request) {
        PageUtil.start(request);
        return leaseBusMapper.queryLeaseBusListForGjSh(request.getQuery());
    }

    @Override
    public CommonResponse gjsh(LeaseBusAndRent leaseBusAndRent) throws Exception {
        LeaseBus leaseBus = leaseBusAndRent.getLeaseBus();
        Rent rent = leaseBusAndRent.getRent();
        rent.setId(leaseBus.getRentId());
        //换房=退租+入住
        //退租=====================================================start
        try{
            woniuBusService.rentOutHouse(rent);
            //调用 账单接口=======扣钱==================
            try{
                billService.deductionBill(rent.getId());
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            return CommonResponse.buildExceptionResponse(e);
        }
        //退租=====================================================end
        //入住=====================================================start
        if(leaseBus.getBusType()==2){//换房
            try{
                //重新入住新的房间
                TenantVo tenantVo = buildTenantVo(rent,
                        leaseBus.getHouseId());
                tenantVo.setRentId(rent.getId());
                try{
                    woniuBusService.rentHouse(tenantVo);
                }catch (CoreException e){
                    return CommonResponse.buildBadRequestResponse(e.getMessage(),null);
                }
                try{
                    billService.regitWallet(rent.getTenantId());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }catch (Exception e){
                return CommonResponse.buildExceptionResponse(e);
            }
        }
        //入住=====================================================end
        //更新leaseBus数据
        leaseBus.setBusState(LeaseBusStateCons.HOMEKEEPER_REVIEW);
        leaseBusMapper.updateByPrimaryKey(leaseBus);
        return CommonResponse.buildUpdateSuccessResponse("管家审核成功!",null);
    }

    /**
     * 组建TenantVo对象
     * @param rent
     * @return
     */
    public TenantVo buildTenantVo(Rent rent,Long houseId){
        Tenant tenant = tenantMapper.selectByPrimaryKey(rent.getTenantId());
        TenantVo tenantVo = JSON.parseObject(JSON.toJSONString(rent),TenantVo.class);
        tenantVo.setName(rent.getTenantName());
        tenantVo.setPhone(rent.getTenantTel());
        //沿用所属公司
        tenantVo.setFormCompy(tenant.getFormCompy());
        //入住操作人
        tenantVo.setOpInId(rent.getOpOutId());
        //新房间号
        tenantVo.setHouseId(houseId);
        return tenantVo;
    }
}
